package com.john.springboot;

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.resources.IgniteInstanceResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement
@SpringBootApplication
@ComponentScan(basePackages={"com.john.springboot","com.john.cache"})  
public class Application {

    protected final static Logger logger = LoggerFactory.getLogger(Application.class);
	@IgniteInstanceResource
	transient Ignite ignite;

	@Autowired
	IgniteConfiguration igniteConfiguration;

	public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Application.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
//		SpringApplication.run(Application.class, args);
        logger.info("PortalApplication is success!");
        System.err.println("sample started. http://localhost:8089/user/all");
    }
    
    
	/**
	 *
	 * @return
	 */
//	@Bean
	public CommandLineRunner runIgnite() {
		return new CommandLineRunner() {
			public void run(String... args) throws Exception {

				Ignite ignite = Ignition.getOrStart(igniteConfiguration);
				// Put values in cache.
				IgniteCache<Integer, String> cache = ignite.getOrCreateCache("test-cache");
				cache.put(1, "Hello");
				cache.put(2, "World!");
				// Get values from cache
				// Broadcast 'Hello World' on all the nodes in the cluster.
				ignite.compute().broadcast(() -> System.out.println(cache.get(1) + " " + cache.get(2)));

			}
		};
	}


}
